import Vue from 'vue'
import VueRouter from 'vue-router'
// 组件
const Login = () => import('../pages/Login/index.vue')
const Layout = () => import('../pages/Layout/index.vue')
const Content = () => import('../pages/Content/index.vue')
const Article = () => import('../pages/Article/index.vue')
const Comment = () => import('../pages/Comment/index.vue')
const Home = () => import('../pages/Home/index.vue')
const Image = () => import('../pages/Image/index.vue')
const Settings = () => import('../pages/Settings/index.vue')
const Fans = () => import('../pages/Fans/index.vue')

Vue.use(VueRouter)

const routes = [{
  path: '/',
  // redirect:'/home',
  name: 'layout',
  component: Layout,
  children: [{
    path: 'content',
    name: 'content',
    component: Content

  },
  {
    path: 'article',
    name: 'article',
    component: Article

  },
  {
    path: 'comment',
    name: 'comment',
    component: Comment

  },
  {
    // path: 'home',
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: 'image',
    name: 'image',
    component: Image

  },
  {
    path: 'settings',
    name: 'settings',
    component: Settings

  },
  {
    path: 'fans',
    name: 'fans',
    component: Fans

  }
  ]

}, {
  path: '/login',
  name: 'login',
  component: Login

}]

const router = new VueRouter({
  routes
})
const unAuthPageList = ['/login']
router.beforeEach((to, from, next) => {
  // console.log('发生了路由变化', from.path, '到', to.path)
  // 如果你要去的页面是主页，而你又没有登陆，则回到登陆页
  // if (to.path === '/' || to.path === '/articles') {
  if (unAuthPageList.includes(to.path)) {
    next()
    // if (localStorage.getItem('token')) {
    //   next()
    // }
    // else {
    //   console.log('你要去的是主页，但是你并没有登陆，所以路由导航守卫把你送到的Login')
    //   next('/login')
    // }
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login?redirect=' + to.path)
    }
  }
})
export default router
